package com.nifostasky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.nifostasky.dao.SysCapchaDao;
import com.nifostasky.entity.SysCapcha;
import com.nifostasky.service.SysCapchaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.awt.image.BufferedImage;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Objects;

/**
 * (SysCapcha)表服务实现类
 *
 * @author makejava
 * @since 2021-09-24 10:03:19
 */
@Service("sysCapchaService")
public class SysCapchaServiceImpl extends ServiceImpl<SysCapchaDao, SysCapcha> implements SysCapchaService {
    @Autowired
    @Qualifier("captchaProducer")
    private DefaultKaptcha captchaProducer;

    @Autowired
    @Qualifier("captchaProducerMath")
    private DefaultKaptcha captchaProducerMath;




    @Override
    public BufferedImage getCaptcha(String type,String uuid) {
        //生成的验证码字符串,用户看到 ; 比如存储 运算算式
        String capStr = null;
        //存储到数据库中的字符串 , 用户看不到;比如存储 运算结果;
        String code = null;
        //图片处理对象
        BufferedImage bi = null;


        //如果uuid为空 或 字符长度为零,直接退出方法
        if (Objects.isNull(uuid) || "".equals(uuid)){
            return null;
        }
        // 默认使用 字符验证码
        if (Objects.isNull(type) || "".equals(type)){
            type = "char";
        }

        switch (type){
            case "math":
                //获取生成的数学表达式
                String capText = captchaProducerMath.createText();
                //提取出数学表达式和结果
                capStr = capText.substring(0, capText.lastIndexOf("@"));
                code = capText.substring(capText.lastIndexOf("@") + 1);
                //将 数学表达式 生成 图片
                bi = captchaProducerMath.createImage(capStr);
                break;
            case "char":
                //提取出生成的字符
                capStr = captchaProducer.createText();
                code = capStr;
                //将字符生成图片
                bi = captchaProducer.createImage(capStr);
                break;
            default:
                break;
        }

        //信息入库;如果今后改用reids,逻辑同理
        SysCapcha sysCapcha = new SysCapcha();
        //将验证码入库
        sysCapcha.setCode(code);
        //设置验证码过期时间为五分钟后
        sysCapcha.setExpireTime(Date.from(LocalDateTime.now().plusMinutes(5).atZone(ZoneId.systemDefault()).toInstant()));
        //uuid入库
        sysCapcha.setUuid(uuid);
        baseMapper.insert(sysCapcha);

        return bi;
    }

    @Override
    public Boolean validCaptcha(String uuid, String code) {
        //uuid为空,则直接返回false
        if (Objects.isNull(uuid) || "".equals(uuid)){
            return false;
        }
        //获取验证码,未查询到则返回false; 查询到则使用后立刻删除数据
        SysCapcha captchaEntity = this.getOne(new QueryWrapper<SysCapcha>().lambda()
                .eq(SysCapcha::getUuid,uuid));
        if (captchaEntity == null) {
            return false;
        }
        //删除验证码
        this.removeById(uuid);
        //判断验证码是否匹配
        if (captchaEntity.getCode().equalsIgnoreCase(code) && new Date().before(captchaEntity.getExpireTime())) {
            return true;
        }
        return false;
    }
}
